package com.GB26875_3_2011.message.parser;

import com.GB26875_3_2011.annotation.Parser;
import com.GB26875_3_2011.common.GB26875Constant;
import com.GB26875_3_2011.model.GB26875MessageBody;
import com.GB26875_3_2011.util.ByteUtils;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

import static com.GB26875_3_2011.codec.MessageDecoder.parseHexTime;

/**
 * @author 蒋仕国
 * @projectName GB26875_3_211
 * @ClassName MessageBodyParser_24
 * @date 2025-01-14 15:55
 * @Description: TODO []
 */
@Parser(unitId = 24)
public class MessageBodyParser_24 implements MessageBodyParser {
    @Override
    public GB26875MessageBody parseBody(ByteBuf buffer) {
        MessageBody_24 messageBody_24 = new MessageBody_24();
        messageBody_24.status(ByteUtils.byteToBitArray(buffer.readByte()));
        messageBody_24.setOperatorNumber(buffer.readByte());
        String timeFlagHex = ByteUtils.readSomeByteToHex(6, buffer);
        LocalDateTime localDateTime = parseHexTime(timeFlagHex, GB26875Constant.BASE_YEARS);
        messageBody_24.setRecordTime(localDateTime);
        return messageBody_24;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    public static class MessageBody_24 implements GB26875MessageBody {

        // 复位 【1有 0无】
        private int reset_00;

        //消音 【1有 0无】
        private int mute_01;

        // 手动报警 【1有 0无】
        private int alarm_02;

        // 紧急报警 【1有 0无】
        private int emergencyAlarm_03;

        // 自检 【1有 0无】
        private int selfCheck_04;

        // 查询应答 【1有 0无】
        private int queryAnswer_05;

        // 测试 【1有 0无】
        private int test_06;

        // 预留
        private int reserved_07;

        // 操作员编号 【1个字节】
        private int operatorNumber;

        // 记录时间
        private LocalDateTime recordTime;

        public void status(int[] status) {
            if (status.length != 8) {
                throw new RuntimeException("状态数组长度必须为8");
            }
            this.reset_00 = status[0];
            this.mute_01 = status[1];
            this.alarm_02 = status[2];
            this.emergencyAlarm_03 = status[3];
            this.selfCheck_04 = status[4];
            this.queryAnswer_05 = status[5];
            this.test_06 = status[6];
            this.reserved_07 = status[7];
        }
    }

}
